using Godot;
using System;

public partial class LevelRobotListItem : MarginContainer
{
	#region 子组件
	TextureRect robotIcon;
	Label robotName;
	TextureRect pilotAvatar;
	Label pilotName;
	Label currentHealth;
	Label maxHealth;
	Label currentSprite;
	Label maxSprite;
	Label weapon1;
	Label weapon2;
	UIButton uIButton;
	#endregion

	[Signal]
	public delegate void OnRobotItemPressedEventHandler(Robot robot);

	public override void _Ready()
	{
		base._Ready();

		robotIcon = GetNode<TextureRect>("%RobotIcon");
		robotName = GetNode<Label>("%RobotName");
		pilotAvatar = GetNode<TextureRect>("%PilotAvatar");
		pilotName = GetNode<Label>("%PilotName");
		currentHealth = GetNode<Label>("%CurrentHealth");
		maxHealth = GetNode<Label>("%MaxHealth");
		currentSprite = GetNode<Label>("%CurrentSprite");
		maxSprite = GetNode<Label>("%MaxSprite");
		weapon1 = GetNode<Label>("%Weapon1");
		weapon2 = GetNode<Label>("%Weapon2");
		uIButton = GetNode<UIButton>("%UIButton");
		uIButton.OnPressed += OnPressedHandler;
	}

	void UpdateInfo(Robot r)
	{
		robotIcon.Texture = GameUtil.GetRobotIcon(r.Entity.Icon, r.IsPlayer, r.IsAlly);
		robotName.Text = r.Entity.RobotName;
		pilotAvatar.Texture = GameUtil.GetPilotAvatar(r.Entity.Pilot.Avatar);
		pilotName.Text = r.Entity.Pilot.PilotName;
		currentHealth.Text = r.Entity.CurrentHealth.ToString();
		maxHealth.Text = r.Entity.TotalHealth.ToString();
		currentSprite.Text = r.Entity.CurrentSprite.ToString();
		maxSprite.Text = r.Entity.TotalSprite.ToString();
		weapon1.Text = r.Entity.WeaponList.Count > 0 ? r.Entity.WeaponList[0].WeaponName : "无";
		weapon2.Text = r.Entity.WeaponList.Count > 1 ? r.Entity.WeaponList[1].WeaponName : "无";
	}

	Robot info;
	public void Init(Robot robot)
	{
		info = robot;
		UpdateInfo(robot);
	}

	private void OnPressedHandler(UIButton button)
	{
		EmitSignal(SignalName.OnRobotItemPressed, info);
	}

}
